
K=20;N=212;Iter=30;
% center=[0, 10, 20, 30, 40, 49]+1;% 51张图 mesh编号中心位置
% center=[0, 4, 7, 3, 4, 5]+1;% 51张图 mesh编号中心位置
% all=randperm(N); center=all(1:K);
space=floor(N/K);center=1:space:N;
% center=floor(linspace(1,212,K));
C=[];


for ii=1:20
    costL=zeros(K,1);costR=zeros(K,1);
    label=zeros(N,1);
    L=zeros(K,1);
    R=zeros(K,1);
    %
    label(1:center(1)-1)=1;
    for i=1:center(1)-1
        i
    costL(1)=costL(1)+distance(center(1),i);
    end
    L(1)=1;
    %
    label(center(K):N)=K;
    for i=center(K):N
        i
    costR(K)=costR(K)+distance(center(K),i);
    end
    R(K)=N;

    for i=1:K-1    
        i
        c1=center(i);c2=center(i+1);
        %不使用c1:c2-1的理由是，左侧和右侧是两个不同的概念！！
        for j=c1:c2
            dis1=distance(j,c1);
            dis2=distance(j,c2);
            if dis1<dis2
                label(j)=i;
                costR(i)=costR(i)+dis1;
                R(i)=j;
            else
                label(j)=i+1;
                costL(i+1)=costL(i+1)+dis2;
                if L(i+1)==0
                    L(i+1)=j;
                end
            end
        end
    end
    
    
    for i=1:K
        center(i)=ceil((L(i)+R(i))/2);
    end

    cost=sum(costL+costR);
    C=[C cost];
end
C
% tabulate(label)

% T=[1:212;center(label)]';
% csvwrite(['keyframe',num2str(K),'.csv'],T)